home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Documentation / DirectX9 / directplay.chm / code / xslutil_v2.js < prev    next >
Encoding:
JavaScript  |  2004-09-27  |  6.5 KB  |  213 lines

  1.  
  2. //Creates an isolation layer between the document and the method of tranformation.
  3. //Allows for:
  4. //                The DOM can be manipulated before a trnasform is applied. Example: Insert member data into the page.    
  5. //                A series of transforms can be applied. Example: Transform content, transform xrefs, transform TLAs.
  6. //                Mini transforms can be run on pieces of the document. Example: Just transform the description.
  7. //                Allows the XSL to be cached via LUCache.
  8. //                Allows for any COM based processing such as regular expression SRs, logging, server event notification. Example: Update the index file, update the targets file, strip comments, find "bad words" and write to a log.
  9. //                Allows a document to be constructed from pieces that may be stored seperately. Example: Document may contain description, but parameters may be in source code.
  10. //                Allows data pages to be built via XSL. Example: Instead of "build scripts", just have a set of data files that are built via a transform.
  11.  
  12. var gxmlDoc = new ActiveXObject("Microsoft.XMLDOM");    //The current document reloaded so it can be written to
  13. var gxslDoc = new ActiveXObject("Microsoft.XMLDOM");    //The stylesheet normally used to to trnasform this document
  14. var gErrorMessage;
  15.  
  16. //main is called from the XSL. It returns the transformed document.
  17. function main(oNode)
  18. {
  19. //debugger;
  20.     var sResult="Unknown Failure";
  21.     gxmlDoc = oNode.cloneNode(true);
  22.     if (!gxmlDoc || gxmlDoc.xml == "")
  23.     {
  24.         sResult = "Unable to clone XML doc node";
  25.         return sResult;
  26.     }
  27. //    sDocPath=getPath(oNode.url);        //The path to this document
  28. //    if (!reload(sDocPath))
  29. //    {
  30. //        sResult=gErrorMessage;
  31. //        return sResult;
  32. //    }
  33.     if (!manipulateDOM())                //Any changes made to the document are done here
  34.     {
  35.         sResult=gErrorMessage;
  36.         return sResult;
  37.     }
  38.     sResult=transformDoc();                //Now transform the doc and return the results to the XSL
  39.     return sResult;
  40. }
  41.  
  42. //reloadDocument function reloads this document into a global dom variable. Returns success or failure.
  43. function reload(sPath)
  44. {
  45.     var bResult=true
  46.     gxmlDoc.validateOnParse=false
  47.     gxmlDoc.async=false
  48.     gxmlDoc.resolveExternals=true
  49.     gxmlDoc.load(sPath)
  50.     var parseErr=gxmlDoc.parseError
  51.     if (parseErr.errorCode != 0)
  52.     {
  53.         gErrorMessage= sPath + " " + parseErr.reason + " " + parseErr.line + " " + parseErr.srcText;
  54.         bResult=false;
  55.     }
  56.     return bResult;
  57. }
  58.  
  59. //getPath strips the file protocol from the document URL if it exists
  60. function getPath(sPath)
  61. {
  62.     var sResult=sPath    //default is do nothing
  63.     var length=sPath.length;
  64.     if (sPath.indexOf('file:///')!=(-1))
  65.     {
  66.         sResult= sPath.substr(8,length-8);
  67.     }
  68.     else
  69.     {
  70.         if (sPath.indexOf('file://')!=(-1))
  71.         {
  72.             sResult= sPath.substr(7,length-7);
  73.         }
  74.     }
  75.     return sResult;
  76. }
  77.  
  78. //manipulateDOM alters the original document;
  79. function manipulateDOM()
  80. {
  81.     var sType = gxmlDoc.selectSingleNode("/inetsdk:topic/metadata/@type").text;
  82.     if (sType != "event")
  83.         return insertMemberData();
  84.     else
  85.     {
  86.         var oEventProps = gxmlDoc.selectNodes("/inetsdk:topic/content/eventprops/xref");
  87.  
  88.         for (i = 0; i != oEventProps.length; i++)
  89.         {
  90.             var sID = oEventProps[i].getAttribute("iid");
  91.             if (!sID) sID = "_inet_event_Object"
  92.             var oParNode = gxmlDoc.selectSingleNode("/inetsdk:topic/parent[@id='" + sID + "']");
  93.             if (!oParNode)
  94.                 insertMemberData(sID);
  95.         }
  96.         return true;
  97.     }
  98. }
  99.  
  100. //insertMemberData looks up the member data for this interface
  101. //Specific to interface pages
  102. function insertMemberData(sID)
  103. {
  104.     bNoLuCache = false;
  105.     
  106.     bNoLuCache = "&cache_lookups;"=="1" ? false : true;
  107.     try{
  108.         var luc = new ActiveXObject("LuCache.Lookup");
  109.     } catch( e ) {
  110.         bNoLuCache = true;
  111.     }
  112.     var bResult=true
  113.     if (!sID)
  114.         var docId = gxmlDoc.selectSingleNode("/inetsdk:topic/metadata/@id").text;
  115.     else
  116.         var docId = sID;
  117.     var dataDoc=new ActiveXObject("Microsoft.XMLDOM");
  118.     dataDoc.validateOnParse=true
  119.     dataDoc.async=false
  120.     dataDoc.resolveExternals=true
  121.     sDataPath = "&path_members;"
  122.  
  123.     if( bNoLuCache ){
  124.         dataDoc.load(sDataPath);
  125.         var parseErr=dataDoc.parseError
  126.         if (parseErr.errorCode != 0)
  127.         {
  128.             gErrorMessage= sDataPath + " " + parseErr.reason + " " + parseErr.line + " " + parseErr.srcText;
  129.             bResult=false;
  130.             return bResult;
  131.         }
  132.         var objectData=dataDoc.nodeFromID(docId);
  133.     } else {
  134.         var objectData = new ActiveXObject("Microsoft.XMLDOM");
  135.         objectData.validateOnParse = false;
  136.         objectData.resolveExternals = true;
  137.         var sCleanPath=getPath(gxmlDoc.documentElement.ownerDocument.url);
  138.         if (sCleanPath.indexOf('http://')!=(-1))
  139.         {
  140.             sCleanPath= "";
  141.         }
  142.         else
  143.         {
  144.             sCleanPath=sCleanPath.substr(0,2);
  145.         }
  146.  
  147.         try{
  148.             var sNodeRep = "<!DOCTYPE parent SYSTEM '/production/xml/dtd/symbols_v1.dtd' []>" + luc.LookupId( docId, sCleanPath + sDataPath );
  149.             objectData.loadXML(sNodeRep);
  150.             objectData = objectData.documentElement;
  151.         } catch(e) {
  152.             // peterril:  just added safty.
  153.             gErrorMessage = "Unable to query objectmembers for parent node.";
  154.             bResult = false;
  155.             return bResult;
  156.         }
  157.     }
  158.     if (objectData)
  159.     {
  160.         var topicNode=gxmlDoc.selectSingleNode("/inetsdk:topic");
  161.         var memberData=objectData.cloneNode(true);
  162.         topicNode.appendChild(memberData);
  163.     }
  164.     return bResult;
  165. }
  166.  
  167. //apply the transform and return the result
  168. function transformDoc()
  169. {
  170.     if (!getXSL())
  171.     {
  172.         return gErrorMessage;
  173.     }
  174.     return gxmlDoc.transformNode(gxslDoc);
  175.     //Could send the results to a series of transforms or through a non-xsl transform
  176. }
  177.  
  178. //get the XSL code that is used to transform this document
  179. //This implementation looks at the PI in the document, strips "_v2" from the path and loads the XSL
  180. //This could easily grab the stylesheet(s) from tmap or from nodes in the document itself
  181. function getXSL()
  182. {
  183.     var piNode=gxmlDoc.childNodes(1);
  184.     var piText=piNode.text
  185.     var piAsDomNode="<pinode " + piText + "/>"
  186.     var piDoc=new ActiveXObject("Microsoft.XMLDOM"); 
  187.     piDoc.loadXML(piAsDomNode);
  188.     var parseErr=piDoc.parseError;
  189.     if (parseErr.errorCode != 0)
  190.     {
  191.         gErrorMessage= "Error loading XSL PI node " + parseErr.reason + " " + parseErr.line + " " + parseErr.srcText;
  192.         bResult=false;
  193.         return bResult;
  194.     }
  195.     piPath=piDoc.documentElement.getAttribute("href");
  196.     piPath=piPath.substring(0, piPath.lastIndexOf("/")) + "/" + "&var_xslpath;";
  197.     //piPath=piPath.replace("_v2","");
  198.  
  199.     gxslDoc.validateOnParse=false
  200.     gxslDoc.async=false
  201.     gxslDoc.resolveExternals=true
  202.     gxslDoc.load(piPath);
  203.     var parseErr=gxslDoc.parseError
  204.     if (parseErr.errorCode != 0)
  205.     {
  206.         gErrorMessage= piPath + " " + parseErr.reason + " " + parseErr.line + " " + parseErr.srcText;
  207.         bResult=false;
  208.         return bResult;
  209.     }
  210.     return true;
  211. }
  212.  
  213.